(function () {
    "use strict";

    angular.module('core').controller('vendaVarejoDetailController', controller);
    controller.$inject = ['$scope', '$timeout', '$state', '$stateParams', 'vendaVarejoService'];

    function controller($scope, $timeout, $state, $stateParams, vendaVarejoService) {
        $scope.initializeLayout('Venda de Varejo');

        //[Inicializa objetos]
        $scope.state = {
            item: { ID: decodeURIComponent($stateParams.id) },
            carregando: false,
            vazio: false,
            tabCorrente: 0,
            tabs: { Itens: 0, Detalhes: 1 },
        };
        $scope.popups = {
            item: {
                selectedIndex: -1,
                selectedItem: null,
            }
        };

        //[Inicializa funcões]
        $scope.initialize = function () {
            $scope.refresh();
        };

        $scope.get = function () {
            $scope.state.carregando = true;
            $scope.state.vazio = false;
            vendaVarejoService
                .get($scope.state.item)
                .then(
                    function (response) {
                        if (response.Status < 0) {
                            $scope.tools.toastr.error(response.Message, 'Serviço');
                            console.error(response.Exception);
                        } else {
                            $scope.state.item = response.Data;
                        }
                        $scope.state.carregando = false;
                        $scope.state.vazio = !$scope.state.item;
                    },
                    function (_error) {
                         
                        $scope.state.carregando = false;
                        $scope.tools.toastr.error('Não foi possível acessar o(s) servidor(es)');
                        console.error(_error);
                    }
                );
        };
        $scope.refresh = function () {
            $scope.get();
        };

        $scope.selectItem = function (index) {
            $scope.popups.item.selectedIndex = index;
            $scope.popups.item.selectedItem = $scope.state.item.Itens[$scope.popups.item.selectedIndex];
        }
        $scope.unselectItem = function () {
            $scope.popups.item.selectedIndex = -1;
            $scope.popups.item.selectedItem = null;
        };
        $scope.nextItem = function () {
            if ($scope.popups.item.selectedIndex < ($scope.state.item.Itens.length - 1)) {
                $scope.popups.item.selectedIndex++;
                $scope.popups.item.selectedItem = $scope.state.item.Itens[$scope.popups.item.selectedIndex];
            }
        };
        $scope.priorItem = function () {
            if ($scope.popups.item.selectedIndex > 0) {
                $scope.popups.item.selectedIndex--;
                $scope.popups.item.selectedItem = $scope.state.item.Itens[$scope.popups.item.selectedIndex];
            }
        };

        /*Inicializa funções*/
        $scope.initialize();

    }

})();